var log = require('../../../../log.js')

Page({
  data: {
    list: [],
    radio: '非纯音乐、粤语、中文或英文歌',
    searchKey: '',
    emptySearch: false,
    showReturnList: false,
    canReachBottom: true
  },
  onLoad() {
    this.loadModal()
    wx.cloud.callFunction({
        name: 'adminAuth'
      })
      .then(res => {
        console.log('adminAuth', res.result)
        if (res.result) {
          this.setData({
            isAdmin: true,
            loadModal: false
          })
        } else {
          this.setData({
            loadModal: false
          })
          wx.reLaunch({
            url: '/pages/index/index'
          })
        }
      })
    this.getList()
  },
  searchInput(e) {
    this.setData({
      searchKey: e.detail.value
    })
  },
  search() {
    this.setData({
      emptySearch: false
    })
    if (this.data.searchKey == '') {
      wx.showToast({
        title: '请输入搜索内容',
        icon: 'none',
        duration: 1500
      })
    } else {
      wx.cloud.callFunction({
          name: 'songSearch',
          data: {
            key: this.data.searchKey
          }
        }).then(res => {
          console.log(res)
          log.info(res)
          this.setData({
            list: res.result.data,
            canReachBottom: false,
            showReturnList: true
          })
          if (res.result.data.length == 0) { //搜索结果为空
            this.setData({
              emptySearch: true
            })
          }
        })
        .catch(err => {
          console.log(err)
          log.error(err)
        })
    }
  },
  returnList() {
    this.setData({
      canReachBottom: true,
      showReturnList: false,
      emptySearch: false,
      list: []
    })
    this.getList()
  },
  onPullDownRefresh() {
    this.loadModal()
    wx.cloud.callFunction({
        name: 'getSongList',
        data: {
          len: 0,
          pageNum: 40
        }
      })
      .then(res => {
        console.log(res.result.data)
        let dataList = res.result.data
        let lastId = this.data.list[0]._id
        for (var i = 0; i < dataList.length; i++) {
          console.log(dataList[i])
          if (dataList[i]._id != lastId) {
            this.setData({
              list: [dataList[i]].concat(this.data.list)
            })
            console.log('this.data.list', this.data.list)
          } else {
            console.log('跳出循环')
            break
          }
        }
        this.setData({
          loadModal: false,
        })
        wx.stopPullDownRefresh()
      })
      .catch(err => {
        this.setData({
          loadModal: false
        })
      })
  },
  getList() {
    this.loadModal()
    let len = this.data.list.length
    wx.cloud.callFunction({
        name: 'getSongList',
        data: {
          len: len,
          pageNum: 30
        }
      })
      .then(res => {
        console.log(res.result.data)
        log.info(res)
        let dataList = res.result.data
        this.setData({
          list: this.data.list.concat(dataList),
          loadModal: false,
        })
        if (dataList.length <= 0) {
          wx.showToast({
            icon: 'none',
            title: '没有更多数据啦'
          })
        }
      })
      .catch(err => {
        this.setData({
          loadModal: false
        })
        log.error(err)
      })
  },
  onReachBottom() {
    if (this.data.canReachBottom) {
      this.getList()
    } else {
      console.log('can not reach bottom')
    }
  },
  songPass(e) {
    this.loadModal()
    wx.cloud.callFunction({
      name: 'changeSongState',
      data: {
        id: e.currentTarget.dataset.id,
        state: 1
      }
    }).then(res => {
      wx.cloud.callFunction({
          name: 'sendSongCheckMessage',
          data: {
            openid: e.currentTarget.dataset.openid,
            title: e.currentTarget.dataset.title,
            singer: e.currentTarget.dataset.singer,
            result: '已通过',
            tip: '前往小程序看看吧！'
          }
        })
        .then(res => {
          console.log(res)
          log.info(res)
          var i = "list[" + e.currentTarget.dataset.index + "].state"
          this.setData({
            [i]: 1,
            loadModal: false
          })
        })
        .catch(err => {
          console.log(err)
          log.error(err)
        })
      console.log('状态更新成功', res)
    }).catch(err => {
      console.log('状态更新失败', err)
      log.error(err)
    })
  },
  songNoPass(e) {
    this.setData({
      postId: e.currentTarget.dataset.id,
      postOpenid: e.currentTarget.dataset.openid,
      postTitle: e.currentTarget.dataset.title,
      postSinger: e.currentTarget.dataset.singer,
    })
    wx.cloud.callFunction({
      name: 'changeSongState',
      data: {
        id: this.data.postId,
        state: 2
      }
    }).then(res => {
      log.info(res)
      this.setData({
        modelShow: true
      })
      console.log('状态更新成功', res)
      var i = "list[" + e.currentTarget.dataset.index + "].state"
      this.setData({
        [i]: 2,
        loadModal: false
      })
    }).catch(err => {
      console.log('状态更新失败', err)
      log.error(err)
    })
  },
  onConfirm(e) {
    console.log(e.currentTarget.dataset.openid)
    wx.cloud.callFunction({
        name: 'sendSongCheckMessage',
        data: {
          openid: this.data.postOpenid,
          title: this.data.postTitle,
          singer: this.data.postSinger,
          result: '不通过',
          tip: this.data.radio
        }
      })
      .then(res => {
        console.log(res)
        log.info(res)
      })
      .catch(err => {
        console.log(err)
        log.error(err)
      })
  },
  onChange(e) {
    this.setData({
      radio: e.detail,
    })
  },
  onClose() {
    this.setData({
      modelShow: false
    })
  },
  //跳转详情页
  goDetail(e) {
    console.log('点击了跳转点歌详情', e.currentTarget.dataset.id)
    wx.navigateTo({
      url: '/pages/songListDetail/songListDetail?id=' + e.currentTarget.dataset.id
    })
  },
  // ListTouch触摸开始
  ListTouchStart(e) {
    this.setData({
      ListTouchStart: e.touches[0].pageX
    })
  },
  // ListTouch计算方向
  ListTouchMove(e) {
    this.setData({
      ListTouchDirection: e.touches[0].pageX - this.data.ListTouchStart > 0 ? 'right' : 'left'
    })
  },
  // ListTouch计算滚动
  ListTouchEnd(e) {
    if (this.data.ListTouchDirection == 'left') {
      this.setData({
        modalName: e.currentTarget.dataset.target
      })
    } else {
      this.setData({
        modalName: null
      })
    }
    this.setData({
      ListTouchDirection: null
    })
  },
  loadModal() {
    this.setData({
      loadModal: true
    })
    var timerTem = setTimeout(() => {
      this.setData({
        loadModal: false
      })
    }, 700)
    var that = this
    that.setData({
      timer: timerTem
    })
  },
  onUnload() {
    console.log("+++++++++onUnload++++++++++")
    clearInterval(this.data.timer) //销毁定时器
  }
})